local Lang = require("common.Lang")
local ShopConfig = require("excel.Shop")
local MysteryRmbShopConfig = ShopConfig.mysteryRmb
local MysteryHonorShopConfig = ShopConfig.mysteryHonor
local MysteryRmbShopFlushConfig = ShopConfig.mysteryRmbVipFlush
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local VipLogic = require("vip.VipLogic")
local VipDefine = require("vip.VipDefine")
local Log = require("common.Log")
local ShopHelp = require("shop.ShopHelp")

MySteryShopList = MySteryShopList or {}
local RmbShopLen = 10
Shop1Len = Shop1Len or 0
Shop2Len = Shop2Len or 0


function onCheck(shopConfigIndex, nIndexData, type, subType)
    if type == 1 then
        if not MysteryRmbShopConfig[shopConfigIndex] or MysteryRmbShopConfig[shopConfigIndex].itemid ~= nIndexData.itemID then
            return false
        end
    elseif type == 2 then
        if not MysteryHonorShopConfig[shopConfigIndex] or MysteryHonorShopConfig[shopConfigIndex].itemid ~= nIndexData.itemID or MysteryHonorShopConfig[shopConfigIndex].type ~= subType then
            return false
        end
    elseif type == 3 then
        if not MysteryHonorShopConfig[shopConfigIndex] or MysteryHonorShopConfig[shopConfigIndex].itemid ~= nIndexData.itemID or MysteryHonorShopConfig[shopConfigIndex].type ~= subType then
            return false
        end
    end
    return true
end

function CG_MYSTERY_SHOP_QUERY(human, msg)
    local nShopList = {}
    local nMsg = nil
    if msg.type == 1 then
        if human.db.mysteryRmbShopListLen == 0 then
            OnFlushMySteryShop(human, 1, true)
        end
        nMsg = Msg.gc.GC_MYSTERY_RMBSHOP_QUERY
        nMsg.type = msg.type
        nMsg.flushRmb = getFlushRmb(human)
        nMsg.shopitems[0] = 0
        nShopList = human.db.mysteryRmbShopList
        for i = 1, RmbShopLen do
            local nShopConfigIndex = nShopList[i - 1].index
            if onCheck(nShopConfigIndex, nShopList[i - 1], 1) ~= true then
                nShopConfigIndex = OnFlushMySteryShopIndex(human, msg.type, i - 1)
            end
            nMsg.shopitems[0] = nMsg.shopitems[0] + 1
            local nData = nMsg.shopitems[nMsg.shopitems[0]]
            nData.type = 1
            nData.index = i - 1
            nData.price1 = MysteryRmbShopConfig[nShopConfigIndex].comsume1
            nData.price2 = MysteryRmbShopConfig[nShopConfigIndex].comsume2
            nData.state = nShopList[i - 1].state
            local nGrid = {}
            Grid.create(nGrid, MysteryRmbShopConfig[nShopConfigIndex].itemid, MysteryRmbShopConfig[nShopConfigIndex].itemcnt)
            Grid.makeItem(nGrid, nData.itemData, 0)
        end
    elseif msg.type == 2 then
        if human.db.mysteryHonorShopListLen == 0 then
            OnFlushMySteryShop(human, 2, true)
        end
        nMsg = Msg.gc.GC_MYSTERY_HONORSHOP_QUERY
        nMsg.type = msg.type
        nMsg.flushRmb = 0
        nMsg.shopitems[0] = 0
        nShopList = human.db.mysteryHonorShopList
        --nShopListLen = human.db.mysteryHonorShopListLen
        for i = 1, Shop1Len do
            local nShopConfigIndex = nShopList[i - 1].index
            if onCheck(nShopConfigIndex, nShopList[i - 1], 2, 1) ~= true then
                OnFlushMySteryShop(human, 2, true) 
            end
            if MysteryHonorShopConfig[nShopConfigIndex] then
                nMsg.shopitems[0] = nMsg.shopitems[0] + 1
                local nData = nMsg.shopitems[nMsg.shopitems[0]]
                nData.type = MysteryHonorShopConfig[nShopConfigIndex].type
                nData.index = i - 1
                nData.price = MysteryHonorShopConfig[nShopConfigIndex].price
                nData.max_cnt = MysteryHonorShopConfig[nShopConfigIndex].limitcount
                nData.alg_cnt = nShopList[i - 1].curCount or 0
                nData.state = nShopList[i - 1].state
                local nGrid = {}
                Grid.create(nGrid, MysteryHonorShopConfig[nShopConfigIndex].itemid, MysteryHonorShopConfig[nShopConfigIndex].itemcnt)
                Grid.makeItem(nGrid, nData.itemData, 0)
            end
        end
    elseif msg.type == 3 then
        if human.db.mysteryPrestigeShopListLen == 0 then
            OnFlushMySteryShop(human, 3, true)
        end
        nMsg = Msg.gc.GC_MYSTERY_HONORSHOP_QUERY
        nMsg.type = msg.type
        nMsg.flushRmb = 0
        nMsg.shopitems[0] = 0
        nShopList = human.db.mysteryPrestigeShopList
        --nShopListLen = human.db.mysteryPrestigeShopListLen
        for i = 1, Shop2Len do
            local nShopConfigIndex = nShopList[i - 1].index
            if onCheck(nShopConfigIndex, nShopList[i - 1], 3, 2) ~= true then
                OnFlushMySteryShop(human, 3, true) 
            end
            if MysteryHonorShopConfig[nShopConfigIndex] then
                nMsg.shopitems[0] = nMsg.shopitems[0] + 1
                local nData = nMsg.shopitems[nMsg.shopitems[0]]
                nData.type = MysteryHonorShopConfig[nShopConfigIndex].type
                nData.index = i - 1
                nData.price = MysteryHonorShopConfig[nShopConfigIndex].price
                nData.max_cnt = MysteryHonorShopConfig[nShopConfigIndex].limitcount
                nData.alg_cnt = nShopList[i - 1].curCount or 1
                nData.state = nShopList[i - 1].state
                local nGrid = {}
                Grid.create(nGrid, MysteryHonorShopConfig[nShopConfigIndex].itemid, MysteryHonorShopConfig[nShopConfigIndex].itemcnt)
                Grid.makeItem(nGrid, nData.itemData, 0)
            end
        end
    end

    Msg.send(nMsg, human.fd)
end

function getFlushRmb(human)
    local nFlushCount = human.db.mysteryRmbShopFlushCnt + 1
    if nFlushCount + 1 > #MysteryRmbShopFlushConfig then
        nFlushCount = #MysteryRmbShopFlushConfig
    end
    if not MysteryRmbShopFlushConfig[nFlushCount] then
        return -1
    end
    return MysteryRmbShopFlushConfig[nFlushCount].flushrmb
end

function CG_MYSTERY_SHOP_FLUSH(human, msg)
    local nFlushType = msg.type
    if nFlushType ~= 1 or (nFlushType == 2 or nFlushType == 3) then
        return
    end
    local msgErr = Msg.gc.GC_BROADCAST
    msgErr.type = Lang.MOUSE

    local nNeedRmb = 0
    if nFlushType == 1 then
        local nCanFlushCnt = VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_SECRETSHOP)
        if nCanFlushCnt <= human.db.mysteryRmbShopFlushCnt then
            msgErr.msg = "次数不足"
            Msg.send(msgErr, human.fd)
            return
        end

        nNeedRmb = getFlushRmb(human)
        if nNeedRmb == -1 or ObjHuman.getYuanbao(human) < nNeedRmb then
            msgErr.msg = "元宝不足"
            Msg.send(msgErr, human.fd)
            return
        end
        ObjHuman.decYuanbao(human, nNeedRmb, "mysteryshopFlush")
        if nFlushType == 1 then
            human.db.mysteryRmbShopFlushCnt = human.db.mysteryRmbShopFlushCnt + 1
        end
    end

    OnFlushMySteryShop(human, nFlushType, nil, nNeedRmb)
    CG_MYSTERY_SHOP_QUERY(human, {type = nFlushType})

    if nFlushType == 1 then
        local nMsg = Msg.gc.GC_MYSTERY_SHOP_FLUSH
        nMsg.type = nFlushType
        nMsg.result = 0
        nMsg.alg_cnt = human.db.mysteryRmbShopFlushCnt
        nMsg.flushRmb = getFlushRmb(human)
        Msg.send(nMsg, human.fd)
    end
end

function OnFlushMySteryShopIndex(human, flushType, shopIndex)
    if flushType == 1 then
        local nWithoutList = {}
        local nTemp = 0
        for i = 1, RmbShopLen do
            if onCheck(human.db.mysteryRmbShopList[i - 1].index, human.db.mysteryRmbShopList[i - 1]) then
                nWithoutList[human.db.mysteryRmbShopList[i].itemID] = 1
            end
        end
        local nIndex = OnRandom(MySteryShopList._rmbShop, human, nWithoutList)
        nTemp = MySteryShopList._rmbShop[nIndex]._index
        human.db.mysteryRmbShopList[shopIndex].index = nTemp
        human.db.mysteryRmbShopList[shopIndex].state = human.db.mysteryRmbShopList[shopIndex].state
        human.db.mysteryRmbShopList[shopIndex].itemID = MySteryShopList._rmbShop[nIndex].itemid
        if human.db.mysteryRmbShopListLen < (shopIndex + 1) then
            human.db.mysteryRmbShopListLen = human.db.mysteryRmbShopListLen + 1
        end
        return nTemp
    end
end

function OnFlushMySteryShop(human, flushType, systemFlush, needRmb)
    if flushType == 1 then
        local nWithoutList = {}
        local nTemp = 0
        human.db.mysteryRmbShopListLen = 0
        -- for i = 1, MySteryShopList._rmbShop.maxIndex do
        for i = 1, RmbShopLen do
            if i > 100 then
                break
            end
            local nIndex = OnRandom(MySteryShopList._rmbShop, human, nWithoutList)
            nTemp = MySteryShopList._rmbShop[nIndex]._index
            nWithoutList[MySteryShopList._rmbShop[nIndex].itemid] = 1

            human.db.mysteryRmbShopList[human.db.mysteryRmbShopListLen].index = nTemp
            human.db.mysteryRmbShopList[human.db.mysteryRmbShopListLen].state = 0
            human.db.mysteryRmbShopList[human.db.mysteryRmbShopListLen].itemID = MySteryShopList._rmbShop[nIndex].itemid
            human.db.mysteryRmbShopListLen = human.db.mysteryRmbShopListLen + 1
            if not systemFlush then
                Log.write(Log.LOGID_OSS_ZHNEBAOFLUSH, human._id, human.account, human.name, human.db.lv, MySteryShopList._rmbShop[nIndex].itemid, MysteryRmbShopConfig[nTemp].itemcnt, needRmb)
            end
        end

        if systemFlush then
            human.db.mysteryRmbShopFlushTime = os.time()
            human.db.mysteryRmbShopFlushCnt = 0
        end
    elseif flushType == 2 then
        human.db.mysteryHonorShopListLen = 0
        --for i = 1, MySteryShopList._honorShop.maxIndex do
        for i = 1, Shop1Len do
            if i > 100 then
                break
            end
            nTemp = MySteryShopList._honorShop[i][1]._index
            human.db.mysteryHonorShopList[human.db.mysteryHonorShopListLen].index = nTemp
            human.db.mysteryHonorShopList[human.db.mysteryHonorShopListLen].state = 0
            human.db.mysteryHonorShopList[human.db.mysteryHonorShopListLen].curCount = 0
            human.db.mysteryHonorShopList[human.db.mysteryHonorShopListLen].itemID = MySteryShopList._honorShop[i][1].itemid
            human.db.mysteryHonorShopListLen = human.db.mysteryHonorShopListLen + 1
        end
    elseif flushType == 3 then
        human.db.mysteryPrestigeShopListLen = 0
        --for i = 1, MySteryShopList._prestigeShop.maxIndex do
        for i = 1, Shop2Len do
            if i > 100 then
                break
            end
            nTemp = MySteryShopList._prestigeShop[i][1]._index
            human.db.mysteryPrestigeShopList[human.db.mysteryPrestigeShopListLen].index = nTemp
            human.db.mysteryPrestigeShopList[human.db.mysteryPrestigeShopListLen].state = 0
            human.db.mysteryPrestigeShopList[human.db.mysteryPrestigeShopListLen].curCount = 0
            human.db.mysteryPrestigeShopList[human.db.mysteryPrestigeShopListLen].itemID = MySteryShopList._prestigeShop[i][1].itemid
            human.db.mysteryPrestigeShopListLen = human.db.mysteryPrestigeShopListLen + 1
        end
    end

end

function OnRandom(shopConf, human, nWithoutList)
    local sum = 0
    local nHumanVipLv = VipLogic.getVipLv(human)
    for i = 1 , #shopConf do
        if nHumanVipLv >= shopConf[i].needvip_lv and not nWithoutList[shopConf[i].itemid] then
            sum = sum + shopConf[i].random_pre
        end
    end
    local r = math.random(1, sum)
    sum = 0
    for i = 1 , #shopConf do
        if nHumanVipLv >= shopConf[i].needvip_lv and not nWithoutList[shopConf[i].itemid] then
            sum = sum + shopConf[i].random_pre
            if sum >= r then
                return i
            end
        end
    end
end

function CG_MYSTERY_SHOP_BUY(human, msg)
    local msgErr = Msg.gc.GC_BROADCAST
    msgErr.type = Lang.MOUSE

    local nType = msg.type
    if nType ~= 1 and nType ~= 2 and nType ~= 3 then
        return
    end
    local nBuyGridIndex = msg.index
    if nType == 1 then
        if nBuyGridIndex < 0 or nBuyGridIndex > RmbShopLen then
            return
        end
    elseif nType == 2 then
        if nBuyGridIndex < 0 or nBuyGridIndex > Shop1Len then
            return
        end
    elseif nType == 2 then
        if nBuyGridIndex < 0 or nBuyGridIndex > Shop2Len then
            return
        end
    end

    local nShopItemConfigData = {}
    if nType == 1 then
        if human.db.mysteryRmbShopList[nBuyGridIndex].state == 1 then
            return
        end

        local nConfigDataIndex = human.db.mysteryRmbShopList[nBuyGridIndex].index
        nShopItemConfigData = MysteryRmbShopConfig[nConfigDataIndex]
        if nShopItemConfigData then
            if nShopItemConfigData.comsume2 > ObjHuman.getYuanbao(human) then
                msgErr.msg = "元宝不足"
                Msg.send(msgErr, human.fd)
                ShopHelp.onNoRMB(human)
                return
            end

            local canAdd = BagLogic.checkCanAddItem(human, nShopItemConfigData.itemid, nShopItemConfigData.isbind or true, nShopItemConfigData.itemcnt)
            if not canAdd then
                msgErr.msg = "背包空间不足"
                Msg.send(msgErr, human.fd)
                return
            end
            human.db.mysteryRmbShopList[nBuyGridIndex].state = 1
            ObjHuman.decYuanbao(human, nShopItemConfigData.comsume2, "mysteryshopBuy")
        else
            return
        end
    elseif nType == 2 then
        if human.db.mysteryHonorShopList[nBuyGridIndex].state == 1 then
            return
        end

        local nConfigDataIndex = human.db.mysteryHonorShopList[nBuyGridIndex].index
        nShopItemConfigData = MysteryHonorShopConfig[nConfigDataIndex]
        if nShopItemConfigData then
            if nShopItemConfigData.price > ObjHuman.getRongyu(human) then
                msgErr.msg = "荣誉不足"
                Msg.send(msgErr, human.fd)
                return
            end

            local canAdd = BagLogic.checkCanAddItem(human, nShopItemConfigData.itemid, nShopItemConfigData.isbind or true, nShopItemConfigData.itemcnt)
            if not canAdd then
                msgErr.msg = "背包空间不足"
                Msg.send(msgErr, human.fd)
                return
            end

            human.db.mysteryHonorShopList[nBuyGridIndex].curCount = human.db.mysteryHonorShopList[nBuyGridIndex].curCount + 1
            ObjHuman.decRongyu(human, nShopItemConfigData.price, "mysteryshopBuy")
            if human.db.mysteryHonorShopList[nBuyGridIndex].curCount >= nShopItemConfigData.limitcount then
                human.db.mysteryHonorShopList[nBuyGridIndex].state = 1
            end
        else
            return
        end
    elseif nType == 3 then
        if human.db.mysteryPrestigeShopList[nBuyGridIndex].state == 1 then
            return
        end

        local nConfigDataIndex = human.db.mysteryPrestigeShopList[nBuyGridIndex].index
        nShopItemConfigData = MysteryHonorShopConfig[nConfigDataIndex]
        if nShopItemConfigData then
            if nShopItemConfigData.price > ObjHuman.getPrestige(human) then
                msgErr.msg = "威望不足"
                Msg.send(msgErr, human.fd)
                return
            end

            local canAdd = BagLogic.checkCanAddItem(human, nShopItemConfigData.itemid, nShopItemConfigData.isbind or true, nShopItemConfigData.itemcnt)
            if not canAdd then
                msgErr.msg = "背包空间不足"
                Msg.send(msgErr, human.fd)
                return
            end

            human.db.mysteryPrestigeShopList[nBuyGridIndex].curCount = human.db.mysteryPrestigeShopList[nBuyGridIndex].curCount + 1
            ObjHuman.decPrestige(human, nShopItemConfigData.price, "mysteryshopBuy")
            if human.db.mysteryPrestigeShopList[nBuyGridIndex].curCount >= nShopItemConfigData.limitcount then
                human.db.mysteryPrestigeShopList[nBuyGridIndex].state = 1
            end
        else
            return
        end

    end

    msgErr.msg = "购买成功"
    Msg.send(msgErr, human.fd)

    if ItemDefine.isEquip(nShopItemConfigData.itemid) then
        local nGridData = Grid.create(nil, nShopItemConfigData.itemid, nShopItemConfigData.itemcnt, nShopItemConfigData.isbind or true)
        BagLogic.addItemByGrid(human, nil, nGridData, "mysteryshopBuy")
    else
        BagLogic.addItem(human, nShopItemConfigData.itemid, nShopItemConfigData.isbind or true, nShopItemConfigData.itemcnt, "mysteryshopBuy")
    end

    local msgRet = Msg.gc.GC_MYSTERY_SHOP_BUY
    msgRet.type = msg.type
    msgRet.index = nBuyGridIndex
    Msg.send(msgRet, human.fd)
    Log.write(Log.LOGID_OSS_ZHNEBAO, human._id, human.account, human.name, human.db.lv, nShopItemConfigData.itemid, nShopItemConfigData.itemcnt, nType, nShopItemConfigData.price or nShopItemConfigData.comsume2)
end

function CG_MYSTERY_SHOP_FLUSHDATA(human, msg)
    if msg.type ~= 1 then
        return
    end
    local nMsg = Msg.gc.GC_MYSTERY_SHOP_FLUSHDATA
    nMsg.type = msg.type
    local nVipLv = VipLogic.getVipLv(human)
    if msg.type == 1 then
        nMsg.alg_cnt = human.db.mysteryRmbShopFlushCnt
        nMsg.max_cnt = nVipLv <= 0 and 0 or VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_SECRETSHOP)
    end
    nMsg.flush_consume = getFlushRmb(human)
    Msg.send(nMsg, human.fd)
end

function OnInit()
    MySteryShopList = {}
    --for k, v in ipairs(MysteryRmbShopConfig) do
    --    if not MySteryShopList._rmbShop then
    --        MySteryShopList._rmbShop = {}
    --    end
    --    if not MySteryShopList._rmbShop[v.index] then
    --        MySteryShopList._rmbShop[v.index] = {}
    --        MySteryShopList._rmbShop.maxIndex = (MySteryShopList._rmbShop.maxIndex or 0) + 1
    --    end
    --    v._index = k
    --    table.insert(MySteryShopList._rmbShop[v.index], v)
    --end
    for k, v in ipairs(MysteryRmbShopConfig) do
        if not MySteryShopList._rmbShop then
            MySteryShopList._rmbShop = {}
        end
        v._index = k
        table.insert(MySteryShopList._rmbShop, v)
    end

    Shop1Len = 0 
    Shop2Len = 0 
    for k, v in ipairs(MysteryHonorShopConfig) do
        if v.type == 1 then
            if not MySteryShopList._honorShop then
                MySteryShopList._honorShop = {}
            end
            if not MySteryShopList._honorShop[v.index] then
                MySteryShopList._honorShop[v.index] = {}
                --MySteryShopList._honorShop.maxIndex = (MySteryShopList._honorShop.maxIndex or 0) + 1
            end
            v._index = k
            table.insert(MySteryShopList._honorShop[v.index], v)
            Shop1Len = Shop1Len + 1
        elseif v.type == 2 then
            if not MySteryShopList._prestigeShop then
                MySteryShopList._prestigeShop = {}
            end
            if not MySteryShopList._prestigeShop[v.index] then
                MySteryShopList._prestigeShop[v.index] = {}
                --MySteryShopList._prestigeShop.maxIndex = (MySteryShopList._prestigeShop.maxIndex or 0) + 1
            end
            v._index = k
            table.insert(MySteryShopList._prestigeShop[v.index], v)
            Shop2Len = Shop2Len + 1
        end
    end
end

function OnDayChange(human)
    if human then
        human.db.mysteryRmbShopFlushTime = os.time()
        human.db.mysteryRmbShopFlushCnt = 0

        OnFlushMySteryShop(human, 1, true)
        OnFlushMySteryShop(human, 2, true)
        OnFlushMySteryShop(human, 3, true)

        CG_MYSTERY_SHOP_QUERY(human, {type = 1})
        CG_MYSTERY_SHOP_QUERY(human, {type = 2})
        CG_MYSTERY_SHOP_QUERY(human, {type = 3})
        CG_MYSTERY_SHOP_FLUSHDATA(human, {type = 1})
    end
end

OnInit()
